//
//  NetworkHelper.m
//  QuicklyPlay
//
//  Created by 你的温柔是我唯一沉溺 on 15/11/1.
//  Copyright © 2015年 OPteam. All rights reserved.
//

#import "NetworkHelper.h"
#import "SVProgressHUD.h"


static NetworkHelper *network;

@interface NetworkHelper ()


@property (strong, nonatomic) NSMutableArray *videoArray;

@property (strong, nonatomic) NSMutableArray *albumArray;

@end

@implementation NetworkHelper




+(NetworkHelper *)sharedInstance {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        network = [[NetworkHelper alloc ]init];
    });
    return network;
}


- (NSMutableArray *)videoArray {
    if (!_videoArray) {
        _videoArray = [NSMutableArray array];
    }
    return _videoArray;
}

- (NSMutableArray *)albumArray {
    if (!_albumArray) {
        _albumArray = [NSMutableArray array];
    }
    return _albumArray;
}

- (NSMutableArray *)loadArray {
    if (!_loadArray) {
        _loadArray = [NSMutableArray array];
    }
    return _loadArray;

}


+(BOOL)judgeNetworkStatus {
    
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    NetworkStatus status = [reach currentReachabilityStatus];
    if (status == NotReachable) {
        
        UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络似乎断开了呢" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        
        [alertV show];
        return NO;
    } else {
        return YES;
    }
}


- (void)downloadWithURL:(NSString *)urlString WithRequestAllDataDidFinish:(void (^)(BOOL))result  {
    
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    NetworkStatus status = [reach currentReachabilityStatus];
    if (status == NotReachable) {
        
        UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络似乎断开了呢" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        
        [alertV show];
        result(NO);
    } else {
        
        //创建菊花
        [SVProgressHUD showWithStatus:@"数据加载中" maskType:SVProgressHUDMaskTypeBlack];
        
        if ([urlString isEqualToString:k_HotsURL]) {
            
            //AFN 网络数据请求
            
            //self.allVideosArray = [[NSArray alloc] init];
            [self.videoArray removeAllObjects];
            AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
            
            [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
                
                NSDictionary *dataDict = responseObject[@"data"];
                
                NSArray *dataArray = dataDict[@"videos"];
                
                for (NSDictionary *dict in dataArray) {
                    
                    VideosModel *videosModel = [[VideosModel alloc] init];
                    
                    [videosModel setValuesForKeysWithDictionary:dict];
                    
                    [self.videoArray addObject: videosModel];
                    
                }
                //将请求到的数据赋值给单例数据接口
                self.allVideosArray = [self.videoArray copy];
                
                
                
                //回到主线程
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [SVProgressHUD showSuccessWithStatus:@"数据加载成功" duration:1];
                    BOOL status = YES;
                    result(status);
                });
                
                
            } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
                
                [SVProgressHUD showErrorWithStatus:@"数据解析出错" duration:1];
                BOOL status = NO;
                result(status);
                
                NSLog(@"网络出错(工具类打印)");
            }];
            
        } else if ([urlString isEqualToString:k_AblumURL]) {
            
            //AFN 网络数据请求
            
            [self.albumArray removeAllObjects];
            
            AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
            
            [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
                
                
                NSDictionary *dataDict = responseObject[@"data"];
                
                NSArray *dataArray = dataDict[@"albums"];
                
                for (NSDictionary *dict in dataArray) {
                    AlbumModel *albumModel = [[AlbumModel alloc] init];
                    
                    [albumModel setValuesForKeysWithDictionary:dict];
                    
                    [self.albumArray addObject: albumModel];
                    
                }
                
                //将请求到的数据赋值给单例数据接口
                
                self.allAlbumArray = [self.albumArray copy];
                
                //回到主线程
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [SVProgressHUD showSuccessWithStatus:@"数据加载成功" duration:1];
                    BOOL status = YES;
                    result(status);
                });
                
            } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
                
                [SVProgressHUD showErrorWithStatus:@"数据解析出错" duration:1];
                BOOL status = NO;
                result(status);
                
                NSLog(@"网络出错(工具类打印)");
            }];
            
        }
    }
    
    
    
    
    
   
}




+(void)getMoreDataWithURL:(NSString *)urlString WithRequestAllDataDidFinishSuccess:(void (^)(id array))success failture:(void (^)(id error))failture {
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        NSDictionary *dataDict = responseObject[@"data"];
        
        NSArray *dataArray = dataDict[@"videos"];
        
        if (success) {
            success(dataArray);
            
        }

    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        
        if(failture){
            failture(error);
        }
    }];

}



//+(BOOL)isConnection {
//    
//        [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//
//            
//        }];
//    
//    
//    
//}







@end
